package com.bawei.day11_contontprovider_a;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyProvider extends ContentProvider {
    private MyHelper myHelper;
    private SQLiteDatabase sqLiteDatabase;
    //MyProvider被创建：获得数据库
    @Override
    public boolean onCreate() {
        myHelper = new MyHelper(getContext());
        sqLiteDatabase = myHelper.getWritableDatabase();
        if(sqLiteDatabase != null){
            return true;
        }
        return false;
    }
    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }
    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        return sqLiteDatabase.query("student",projection,selection,selectionArgs,null,null,sortOrder);
    }
    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        sqLiteDatabase.insert("student",null,values);
        return uri;
    }
    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return sqLiteDatabase.delete("student",selection,selectionArgs);
    }
    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return sqLiteDatabase.update("student",values,selection,selectionArgs);
    }
}
